package com.flying.oop.classmember.method;

import java.util.Arrays;

/**
 * @ClassName: MethodTest3
 * @createDate: 2023/6/21 15:56
 * @author: liu
 * @description:知识点:方法的重载
 */
public class MethodTest3 {

    public static void main(String[] args) {

        int index1 = binarySearch(new int[]{24, 27, 53, 47, 50},53 );
        System.out.println(index1);

        int index2 = binarySearch(new int[]{24, 27, 53, 47, 50},50 );
        System.out.println(index2);

        int[] i={1,2,3,4,5,6,7};
        int[] i2 = Arrays.copyOf(i, 3);
    }

    // 使用二分法查找，arr数组中查找value的下标，如果value不存在，就返回-1，
    public static int binarySearch(int[] arr, int value) {
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
        int index=-1;
        int left = 0;
        int right = arr.length - 1;
        while(left<=right) {
            int middle = (left + right) / 2;
            if (value > arr[middle]) {
                left = middle + 1;
            } else if (value < arr[middle]) {
                right = middle - 1;
            }else {
                index=middle;
                break;
            }
        }
        return index;
    }
}

